<script>
    $(function () {
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;

            /**
             * 初始化表单，要加上，不然刷新部分组件可能会不加载
             */
            form.render();

            //日期
            laydate.render({
                elem: '#test1'
            });
            laydate.render({
                elem: '#date1'
            });

            //创建一个编辑器
            var editIndex = layedit.build('LAY_demo_editor');

            //自定义验证规则
            form.verify({
                title: function (value) {
                    if (value.length < 5) {
                        return '标题至少得5个字符啊';
                    }
                }
                , pass: [
                    /^[\S]{6,12}$/
                    , '密码必须6到12位，且不能出现空格'
                ]
                , content: function (value) {
                    layedit.sync(editIndex);
                }
            });

            //监听指定开关
            form.on('switch(switchTest)', function (data) {
                layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                    offset: '6px'
                });
                layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
            });

            //监听提交
            form.on('submit(demo1)', function (data) {
                layer.alert(JSON.stringify(data.field), {
                    title: '最终的提交信息'
                })
                return true;
            });

            //表单初始赋值
            form.val('example', {
                "username": "贤心" // "name": "value"
                , "password": "123456"
                , "interest": 1
                , "like[write]": true //复选框选中状态
                , "close": true //开关状态
                , "sex": "女"
                , "desc": "我爱 layui"
            })

            form.on('select(dep)',function(data){
                var url="/getDepartment?department="+data.value;
                $.get(url,function (data) {
                    var tmp='<option value="">小孩子都知道该怎么选</option>';
                    for (var i in data){
                        tmp +='<option value="'+data[i]+'">'+data[i]+'</option>';
                    }
                    $("#leader").html(tmp);
                    //重载select模块，否则不会展示
                    layui.form.render("select");
                });
            })



        });


    })
</script>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <form class="layui-form" action="/addApply">
            <!--默认的审核状态-->
            <input type="hidden" name="status" value="0" />
            <!--员工的编号-->

            <div class="layui-form-item">
                <label class="layui-form-label">员工ID</label>
                <div class="layui-input-block">
                    <input type="text" name="employeeID" lay-verify="required" lay-reqtext="请填写正确的员工编号" autocomplete="off" placeholder="请输入你的员工的ID" class="layui-input">
                </div>
            </div>
            <!--申请的员工的姓名-->
            <div class="layui-form-item">
                <label class="layui-form-label">你的名字</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required" lay-reqtext="你不填，我怎么知道把钱给谁？" placeholder="请输入你的名字" autocomplete="off" class="layui-input">
                </div>
            </div>
            <!--所属部门-->
            <div class="layui-inline">
                <label class="layui-form-label">所属部门</label>
                <div class="layui-input-inline">
                    <select name="department" lay-verify="required" lay-reqtext="部门不能为空" lay-filter="dep">
                        <option value="人事部">人事部</option>
                        <option value="销售部">销售部</option>
                        <option value="生产部">生产部</option>
                        <option value="财务部">财务部</option>
                        <option value="研发部">研发部</option>
                        <option value="物资部">物资部</option>
                    </select>
                </div>
            </div>
            <!--直系领导-->
            <div class="layui-inline">
                <label class="layui-form-label">直系领导</label>
                <div class="layui-input-inline">
                    <select name="leader" id="leader" lay-verify="required" lay-reqtext="职位不能为空" lay-filter="post">
                    </select>
                </div>
            </div>

            <!--下拉列表选项，选择申请的类型
                日常损耗品、报销(外出住宿车费的报销等)、团建经费、采购
            -->
            <div style="margin-top: 10px">
                <label class="layui-form-label">申请类型</label>
                <div class="layui-input-inline">
                    <select name="applytype">
                        <option value="日常损耗品">日常损耗品</option>
                        <option value="报销">报销(外出住宿车费的报销等)</option>
                        <option value="团建经费">团建经费</option>
                        <option value="采购">采购</option>
                    </select>
                </div>
            </div>
            <!--申请理由-->
            <div class="layui-form-item layui-form-text" style="margin-top: 10px">
                <label class="layui-form-label">申请理由</label>
                <div class="layui-input-block">
                    <textarea name="reason" placeholder="你要钱的借口" class="layui-textarea"></textarea>
                </div>
            </div>
            <!--申请的金额-->
            <div class="layui-form-item">
                <label class="layui-form-label">申请金额</label>
                <div class="layui-input-block">
                    <input type="text" name="money" lay-verify="required" lay-reqtext="这个不填,怎么拿钱" placeholder="公司不富有，但是也不是很穷，不浪费就行" autocomplete="off" class="layui-input">
                </div>
            </div>
            <!--票据，有发票的时候需要上传文件-->
            <div class="layui-form-item">
                <label class="layui-form-label">发票上传</label>
                <div class="layui-input-block">
                    <input type="file" name="bill"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <!--申请日期-->
            <div class="layui-inline">
                <label class="layui-form-label">申请日期</label>
                <div class="layui-inline">
                    <input name="date" lay-verify="required" lay-reqtext="莫急，还差最后一个日期了" type="text" class="layui-input" id="test1">
                </div>
            </div>
            <!--提交按钮-->
            <div class="layui-form-item" style="margin-top: 10px">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>